﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.UsersAndSecurity.IViews;
using MyCommerce.Presentation.UsersAndSecurity.Presenters;
using Innova.Microfinanzas.BusinessEntities;
using Innova.FoundationFramework.DataTypes;
using Innova.Microfinanzas.BusinessEntities.SystemEnums;

namespace MyCommerce.Windows.UsersAndSecurity.Views
{
    public partial class EmployeeFillView : BaseForm, IEmployeeFillView
    {
        private EmployeeFillPresenter pPresenter;

        public EmployeeFillView()
        {
            InitializeComponent();
            pPresenter = new EmployeeFillPresenter(this);
        }

        #region IEmployeeFillView Members

        public long IdItemEdit { get; set; }

        public OptionSelect OptionViewSelect { get; set; }

        public List<DealerStore> ColDealerStores
        {
            set
            {
                DealerStore objNullStore = new DealerStore();
                objNullStore.IdDealerStore = 0;
                objNullStore.Name = "Ninguna";
                value.Add(objNullStore);
                value.Sort((x, y) => x.IdDealerStore.CompareTo(y.IdDealerStore));
                listDealerStores.DisplayMember = "Name";
                listDealerStores.ValueMember = "IdDealerStore";
                listDealerStores.DataSource = value;
            }
        }

        public Employee Get()
        {
            Employee objEmployee = Employee.CreateEmployee(0, fieldName.Text.Trim(), fieldFamilyName1.Text.Trim(), fieldFamilyName2.Text.Trim(),
                fieldIsActive.Checked, fieldPhone.Text, fieldCelularPhone.Text, fieldAddress.Text);
            if (listDealerStores.SelectedIndex != -1
                && Convert.ToInt64(listDealerStores.SelectedValue) > 0)
                objEmployee.IdDealerStore = Convert.ToInt64(listDealerStores.SelectedValue);
            else
                objEmployee.IdDealerStore = null;
            return objEmployee;
        }

        public void Set(Employee objUserToLoad)
        {
            IdItemEdit = objUserToLoad.IdEmployee;
            fieldName.Text = objUserToLoad.Name;
            fieldFamilyName1.Text = objUserToLoad.FamilyName1;
            fieldFamilyName2.Text = objUserToLoad.FamilyName2;
            fieldPhone.Text = objUserToLoad.Phone;
            fieldCelularPhone.Text = objUserToLoad.CellPhone;
            fieldAddress.Text = objUserToLoad.Address;
            fieldIsActive.Checked = objUserToLoad.IsActive;
            if (String.IsNullOrEmpty(listDealerStores.ValueMember) == false
                && objUserToLoad.IdDealerStore.HasValue)
                listDealerStores.SelectedValue = objUserToLoad.IdDealerStore;
            else
                listDealerStores.SelectedIndex = -1;
        }

        public void CloseAndSetResult(bool result)
        {
            if (result)
                Context.ShowInformationMessageBox("La información se guardó correctamente.", "Creación/Edición de usuarios.");
            Result = result;
            Close();
        }

        public event GenericEventHandler<Employee> OnLoadView;

        public event EventHandler OnSave;

        public event EventHandler OnCancel;

        #endregion

        private bool ValidateView()
        {
            bool vResult = true;
            StringBuilder message = new StringBuilder();

            if (string.IsNullOrEmpty(fieldName.Text.Trim()))
            {
                vResult = false;
                message.AppendLine("Debe ingresar el nombre del empleado.");
            }

            if (string.IsNullOrEmpty(fieldFamilyName1.Text.Trim()))
            {
                vResult = false;
                message.AppendLine("Debe ingresar el apellido paterno del empleado.");
            }

            if (string.IsNullOrEmpty(fieldFamilyName2.Text.Trim()))
            {
                vResult = false;
                message.AppendLine("Debe ingresar el apellido materno del empleado.");
            }

            if (string.IsNullOrEmpty(fieldPhone.Text.Trim())
                && string.IsNullOrEmpty(fieldCelularPhone.Text.Trim()))
            {
                if (string.IsNullOrEmpty(fieldPhone.Text.Trim()))
                {
                    vResult = false;
                    message.AppendLine("Debe ingresar el número de teléfono fijo del empleado.");
                }

                if (string.IsNullOrEmpty(fieldCelularPhone.Text.Trim()))
                {
                    vResult = false;
                    message.AppendLine("Debe ingresar el número de celular del empleado.");
                }
            }

            if (string.IsNullOrEmpty(fieldAddress.Text.Trim()))
            {
                vResult = false;
                message.AppendLine("Debe ingresar la dirección del empleado.");
            }

            if (!vResult)
            {
                Context.ShowErrorMessageBox(message.ToString(), "Validación de datos.");
            }

            return vResult;
        }

        private void EmployeeFillView_Load(object sender, EventArgs e)
        {
            Employee objUser = null;
            OptionViewSelect = OptionSelect.Create;
            if (Parameter != null)
            {
                objUser = (Employee)Parameter;
                OptionViewSelect = OptionSelect.Edit;
            }
            if (OnLoadView != null)
                OnLoadView(this, new GenericEventArgs<Employee>(objUser));
        }

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (OnCancel != null)
                OnCancel(this, new EventArgs());
        }

        private void tsSaveButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateView())
                {
                    if (OnSave != null)
                        OnSave(this, new EventArgs());
                }
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }
    }
}
